package core

import (
	"data-transfer/conf"
	"data-transfer/global"
	"encoding/json"
	"go.uber.org/zap"
	"io/ioutil"
)

func GetBinlogSetPosition() conf.Position {
	name := global.GViper.GetString("binlog.name")
	pos := global.GViper.GetInt32("binlog.pos")
	return conf.Position{Name: name, Pos: uint32(pos)}
}

func GetBinlogCachePosition() conf.Position {
	content, err := ioutil.ReadFile("./binlog_position_cache")
	if err != nil {
		global.LOG.Error("First Running Error during opening binlog_position_cache file,skip it! ", zap.Error(err))
		return conf.Position{}
	}

	var payload conf.Position
	err = json.Unmarshal(content, &payload)
	if err != nil {
		global.LOG.Error("First Running Error during Unmarshal Binlog Position,skip it! ", zap.Error(err))
		return conf.Position{}
	}

	return payload

}
